<template>
  <div>
    <div
      class="wrapper-container"
      style="display: flex; flex-direction: column; width: 100%"
    >
      <div class="search-container">
        <div class="search-container-left">
          <div class="search-container-item" style="margin-left: 20px">
            <span>学期：</span>
            <el-select
              v-model="semester"
              style="width: 90px"
              size="mini"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in semesterList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>

          <div class="search-button" @click="getJysPkqktj">
            <img src="../../../../assets/assessment/icon-search.png" alt="" />
            <span>查询</span>
          </div>
        </div>
      </div>
    </div>

    <div class="assessment-container">
      <div class="table-container">
        <el-table
          :data="tableData"
          border
          class="table-data"
          row-class-name="row-class"
          cell-class-name="cell-class"
          header-row-class-name="header-row-class"
          header-cell-class-name="header-cell-class"
          style="height: calc(100vh - 320px); overflow-y: auto"
          :span-method="objectSpanMethod"
        >
          <el-table-column prop="bx" label="部门" align="center">
          </el-table-column>
          <el-table-column prop="jys" label="教研室" align="center">
          </el-table-column>
          <el-table-column prop="zs" label="总教学任务" align="center">
            <template slot-scope="scope">
              <span class="undeLine">{{ scope.row["zs"] }}</span>
              <i
                class="el-icon-edit edit-icon"
                @click="selectTeachersNumber(scope.row, '')"
              ></i>
            </template>
          </el-table-column>
          <el-table-column prop="wfb" label="待发布教学任务" align="center">
            <template slot-scope="scope">
              <span class="undeLine">{{ scope.row["wfb"] }}</span>
              <i
                class="el-icon-edit edit-icon"
                @click="selectTeachersNumber(scope.row,'待发布')"
              ></i>
            </template>
          </el-table-column>
          <el-table-column prop="yfb" label="已发布教学任务" align="center">
            <template slot-scope="scope">
              <span class="undeLine">{{ scope.row["yfb"] }}</span>
              <i
                class="el-icon-edit edit-icon"
                @click="selectTeachersNumber(scope.row, '已发布')"
              ></i>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="wrapper-container" style="width:calc(100% - 20px)">
      <div class="bottom-block" style="display: block">
        <div class="text">当前共找到{{ total }}条记录,当前显示{{ startPage }}-{{ endPage }}条</div>
      </div>
      <div class="export-button" @click="exportData()">
        <img src="../../../../assets/assessment/icon-export.png" alt="" />
        <span>&ensp;导出Excel</span>
      </div>
      <div class="pagination-block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          >
          </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { static_getJysPkqktj, queryCommonXueqi ,static_exportJysPkqktj, queryDefaultXueQi} from "@/api/trainingManagement/place.js";
import ButtonItem from "@/components/ButtonItem.vue";

export default {
  components: {
    ButtonItem,
  },
  data() {
    return {
      total: 0, //总条数
      pageSize: 50, //条数
      startPage: 0, //开始条数
      endPage: 0, //结束条数
      currentPage: 1, //当前页数
      /*对象名称集*/
      parameterNameArr: [],
      semester: "", //学期
      semesterList: [], //学期列表
      tableData: [],
      jsonFields: {
        部门: "bx",
        教研室: "jys",
        总教学任务: "zs",
        待发布教学任务: "wfb",
        已发布教学任务: "yfb",
      },
    };
  },
  created() {},
  mounted() {
    this.getDefaultXueQi();
    this.queryCommonXueqi();
  },
  methods: {
    async getDefaultXueQi(){
      queryDefaultXueQi().then(res=>{
        if(res.data.code == 200){
          this.semester = res.data.data;
        }
      }).then(()=>{
        this.getJysPkqktj();
      })
    },
    exportData() {
      var xueqi = this.semester;
      static_exportJysPkqktj(xueqi).then((res) => {
        /** 获取生成设置好的文件名 */
        /** 接收文件流 */
        const blob = new Blob([res.data]);
        let url = URL.createObjectURL(blob);
        /** 模拟浏览器操作Document，并模拟下载动作 */
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", "教研室排课情况统计.xlsx");
        document.body.appendChild(link);
        link.click();
      });
    },
    // 获取学期列表
    async queryCommonXueqi() {
      const res = await queryCommonXueqi();
      if (res.data.code == 200) {
        this.semesterList = res.data.data;
      }
    },
    // 查询不排课统计
    async getJysPkqktj() {
      var xueqi = this.semester;
      var current = this.currentPage;
      var size = this.pageSize;
      const res = await static_getJysPkqktj(xueqi, current, size);
      if (res.data.code == 200) {
        var data = res.data.data.records;
        var newArr = [];
        data.forEach((item) => {
          if (item.list.length > 0) {
            item.list.forEach((da) => {
              var par = da;
              newArr.push(par);
            });
          }
        });
        this.tableData = newArr;
        this.getSpanArr(this.tableData);
        this.total = res.data.data.total;
        var length = this.tableData.length;
        this.calculatePage(
          this.pageSize,
          res.data.data.current, length);
      }
    },
    // 计算开始和结束条数
    calculatePage(pageSize, current, length) {
      if (current == 1) {
        if (length != 0) {
          this.startPage = 1;
          this.endPage = length;
        }
      } else {
        this.startPage = pageSize * (current - 1) + 1;
        this.endPage = parseInt(this.startPage)  + parseInt(length) - 1;
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getJysPkqktj();
    },
    selectTeachersNumber(row, state) {
      console.log('---------------', row)
      this.$router.push({
        path: "/admin/statistic/teachingTaskDetails",
        query: {
          jysId: row.jysId,
          jys: row.jys,
          // start: this.startDate,
          // end: this.endDate,
          semester: this.semester,
          state: state
        },
      });
    },
    /*获取相同数据进行合并*/
    getSpanArr(data) {
      if (data == null) {
        return;
      } else {
        /*对象名称集*/
        this.parameterNameArr = [];
        /*对象名称相同的个数*/
        this.parameterNamePop = 0;
      }
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          /*对象名称集*/
          this.parameterNameArr.push(1);
          this.parameterNamePop = 0;
        } else {
          if (data[i].bx === data[i - 1].bx) {
            this.parameterNameArr[this.parameterNamePop] += 1;
            this.parameterNameArr.push(0);
          } else {
            this.parameterNameArr.push(1);
            this.parameterNamePop = i;
          }
        }
      }
    },
    //合并单元格
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const row_2 = this.parameterNameArr[rowIndex];
        const col_2 = row_2 > 0 ? 1 : 0;
        return {
          rowspan: row_2,
          colspan: col_2,
        };
      }
    },
  },
  computed: {}, //计算属性
};
</script>
<style lang="less" scoped>
@import "../../../../styles/text.less";
.assessment-container .table-container {
  // overflow: scroll;
}
.assessment-container .table-container .table-header {
  width: 1935px;
}
.assessment-container .table-container .table-body {
  width: 1935px;
}
/deep/ .szjky .el-dialog__body {
  height: 432px;
}
.pagination-block {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: center;
}
</style>
<style lang="less" scoped>
@import "../../../../styles/supervision.less";

/deep/ .table-container {
  .el-table td,
  .el-table th.is-leaf,
  .el-table--border,
  .el-table--group {
    border-color: #c7d7eb;
  }

  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 76px;
    background-color: #edf2f9;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    line-height: 20px;
    color: #004ca7;
  }

  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 86px;
  }

  .wrapper-button {
    display: flex;
    flex-direction: column;
    align-items: center;

    > div {
      margin-bottom: 8px;
    }
  }
  .table-data {
    width: 100%;
    border: 0.8px solid #004ca7;
    /*border-radius: 9px;*/
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #004ca7;
  }
}

.wrapper-container .search-container .search-container-left {
  width: 100%;
  flex-wrap: wrap;
  .search-container-item {
    width: 12%;
    margin-right: 0px;
    .box {
      > span {
        text-align: left;
      }
      display: flex;
      justify-content: space-evenly;
      margin-bottom: 20px;
      align-items: center;
    }
  }
}

/deep/.el-date-editor.el-input {
  width: 110px;
}
/deep/ .el-input--medium {
  width: 110px;
  .el-input__inner {
    height: 28px;
    width: 110px;
    padding: 0px 18px;
  }
}
/deep/ .el-table thead {
  color: #004ca7 !important;
}

.btn {
  margin-top: 271px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 271px;
  .submit {
    width: 84px;
    height: 27px;
    line-height: 8px;
  }
}
  .export-button {
    cursor: pointer;
    width: 127px;
    height: 27px;
    background: #fff6ef;
    border: 1px solid #f3b815;
    border-radius: 4px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-top: 14px;
    > img {
      width: 17px;
      height: 17px;
    }
    > span {
      margin-left: 12px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #f3b815;
    }
  }

.search-button {
  width: 77px;
  height: 28px;
  line-height: 28px;
  background: #f1f8fe;
  border: 1px solid #51aef6;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  > span {
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #51aef6;
  }
  > img {
    width: 20px;
    height: 20px;
    margin-right: 4px;
  }
}

.mar-lef-2 {
  margin-left: 20px;
}
.undeLine {
  text-decoration: underline;
  cursor: pointer;
}

.edit-icon {
  font-size: 16px;
  color: #02c86c;
  margin-left: 10px;
  cursor: pointer;
}

@import "../../../../styles/delayedExam.less";
.pagination-block {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.assessment-container {
  width: calc(100% - 20px);
  .table-container {
     width: 100%;
     margin-top: 20px
  }
}
</style>
